package com.juanLL.CMC.controller;

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.juanLL.CMC.models.GameWorld;
import com.juanLL.CMC.view.GameView;

/**
 * InputController class, implements OnTouchListener to receive and handle Touch
 * events from the device. Handles user input and passes information to the
 * GameWorld object so it can react accordingly.
 * 
 */
public class InputController implements OnTouchListener {
	// log tag constant id for debugging
	public static String TAG = "InputController";
	// The GameWold object that will react to the user's input.
	GameWorld gameWorld;
	// The gameView on which the InputController listens for events
	GameView gameView;

	/**
	 * InputController Constructor
	 * 
	 * @param gameWorld
	 *            the GameWold object that will react to the user's input.
	 * @param gameView
	 *            the gameView on which the InputController listens for events
	 */
	public InputController(GameWorld gameWorld, GameView gameView) {
		this.gameWorld = gameWorld;
		gameView.setOnTouchListener(this);
	}

	/**
	 * onTouch method handles touch events. Called when the user touches the
	 * screen to interact with the game models
	 */
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
		// case user presses the screen
		case MotionEvent.ACTION_DOWN:
			gameWorld.doTouchDown(event.getX(), event.getY());
			return true;
			// case user slides finger across the screen
		case MotionEvent.ACTION_MOVE:
			gameWorld.doTouchMove(event.getX(), event.getY());
			return true;
			// case user lifts the finger after having pressed the screen
		case MotionEvent.ACTION_UP:
			gameWorld.doTouchUp(event.getX(), event.getY());
		}

		return false;
	}

}
